package eclipse.lib.graphics;
import java.util.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;
import eclipse.*;
import eclipse.lib.*;
public class GLParticle 
{	
	private Position position;
	private Position position_copy;
	private Position velocity;
	private Position velocity_copy;
	private Position friction;	
	private Position terminal;	
	private Position spinSpeed;
	private float spin;
	private float rot;
	private int life;	
	private int max_life;
	private float size;
	private Texture texture;	
	private GLColor color;
	private GLColor color_copy;
	public boolean triad = true;
	public boolean isAlive()
	{
		if(life > 0)
			return true;
		else
			return false;
	}
	public void reposition(GL gl)
	{
		if(life > 0)
			life --;
		else
			return;
		if(velocity.x > terminal.x)
			velocity.x -= terminal.x;
		else if(velocity.x < terminal.x)
			velocity.x += friction.x;
		else
			velocity.x = terminal.x;
		
		if(velocity.y > terminal.y)
			velocity.y -= friction.y;
		else if(velocity.y < terminal.y)
			velocity.y += friction.y;
		else
			velocity.y = terminal.y;
		
		if(velocity.z > terminal.z)
			velocity.z -= terminal.z;
		else if(velocity.z < terminal.z)
			velocity.z += terminal.z;
		else
			velocity.z = terminal.z;
		
		position.x += velocity.x;
		position.y += velocity.y;
		position.z += velocity.z;
		rot += spin;
		gl.glPushMatrix();
			gl.glTranslatef(position.x, position.y, position.z);	
			gl.glRotatef(rot, spinSpeed.x, spinSpeed.y, spinSpeed.z);
			gl.glColor3f(color.getRed(), color.getGreen(), color.getBlue());
			if(triad)
				GLPrimitives.drawParticles(gl, size, texture);
			else
				GLPrimitives.drawQuad(gl, Constants.TILERECT, texture);
		gl.glPopMatrix();
	}
	public void setPosition(float x, float y, float z)
	{
		this.position.x = x;
		this.position.y = y;
		this.position.z = z;
		this.position_copy.x = x;
		this.position_copy.y = y;
		this.position_copy.y = y;
	}
	public Position getPosition()
	{
		return position;
	}
	public void setVelocity(float x, float y, float z)
	{
		this.velocity.x = x;
		this.velocity.y = y;
		this.velocity.z = z;
		this.velocity_copy.x = x;
		this.velocity_copy.y = y;
		this.velocity_copy.y = y;
	}
	public Position getVelocity()
	{
		return velocity;
	}
	public void setTerminal(float x, float y, float z)
	{
		this.terminal.x = x;
		this.terminal.y = y;
		this.terminal.z = z;
	}
	public Position getTerminal()
	{
		return terminal;
	}	
	public void setFriction(float x, float y, float z)
	{
		this.friction.x = x;
		this.friction.y = y;
		this.friction.z = z;
	}
	public Position getFriction()
	{
		return friction;
	}
	public void setSize(float size)
	{
		this.size = size;
	}
	public float getSize()
	{
		return size;
	}
	public void setLife(int life)
	{		
		this.life = life;
	}
	public int getLife()
	{
		return life;
	}
	public void setMaxLife(int maxLife)
	{
		max_life = maxLife;
	}
	public int getMaxLife()
	{
		return max_life;
	}
	public void setSpin(float spin)
	{
		this.spin = spin;
	}
	public float getSpin()
	{
		return spin;
	}
	public void setTexture(Texture texture)
	{
		this.texture = texture;
	}
	public Texture getTexture()
	{
		return texture;
	}
	public Position getSpinSpeed()
	{
		return spinSpeed;
	}
	public void setSpinSpeed(float x, float y, float z)
	{
		spinSpeed.x = x;
		spinSpeed.y = y;
		spinSpeed.z = z;
	}
	
	public GLParticle()
	{
		setDefault();
	}
	public GLColor getColor()
	{
		return color;
	}
	public void setColor(float r, float g, float b)
	{
		color.setRed(r);
		color.setGreen(g);
		color.setBlue(b);
		color_copy.setRed(r);
		color_copy.setBlue(b);
		color_copy.setGreen(g);
	}
	public void setDefault()
	{
		position = 		new Position();
		velocity = 		new Position();
		terminal = 		new Position();
		friction = 		new Position();		
		spinSpeed= 		new Position();
		position_copy = new Position();
		velocity_copy = new Position();
		color = 		new GLColor(0f, 0f, 0f);
		color_copy    = new GLColor(0f, 0f, 0f);
		
		setPosition(0f, 0f, 0f);
		setVelocity(0f, 0f, 0f);
		setSpinSpeed(0f, 0f, 1f);
		setTerminal(0f, 0f, 0f);
		setFriction(0f, 0f, 0f);				
	}
	public GLParticle(int life, float size, Texture texture)
	{
		this.life = life;
		this.size = size;
		this.texture = texture;	
		setDefault();
	}
	public GLParticle(int life, float size, Texture texture, Position terminal, Position velocity, Position friction, Position root, Position spinSpeed, float spin, GLColor color)
	{				
		setDefault();
		setSpin(spin);
		setSpinSpeed(spinSpeed.x, spinSpeed.y, spinSpeed.z);
		setPosition(root.x, root.y, root.z);
		setTerminal(terminal.x, terminal.y, terminal.z);
		setVelocity(velocity.x, velocity.y, velocity.z);
		setFriction(friction.x, friction.y, friction.z);
		setLife(life);
		setMaxLife(life);
		setSize(size);
		setTexture(texture);
		setColor(color.getRed(), color.getGreen(), color.getBlue());
		reset();
	}	
	public void reset()
	{		
		Random rand = new Random();
		float colVal = 0;
		position.x = position_copy.x;
		position.y = position_copy.y;
		position.z = position_copy.z;
		velocity.x = velocity_copy.x;
		velocity.y = velocity_copy.y;
		velocity.z = velocity_copy.z;
		life	   = rand.nextInt(max_life);
			
		if(color_copy.getRed() > 0)
		{
			do			
				colVal = rand.nextFloat();
			while(colVal > color_copy.getRed());
			color.setRed(colVal);
		}
		if(color_copy.getBlue() > 0 )
		{
			do
				colVal = rand.nextFloat();
			while(colVal > color_copy.getBlue());
			color.setBlue(colVal);
		}
		if(color_copy.getGreen() > 0)
		{
			do
				colVal = rand.nextFloat();
			while(colVal > color_copy.getGreen());
			color.setGreen(colVal);
		}
	}
	
}
